package com.example.administrator.nurse.base;

import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.administrator.nurse.common;

import java.util.Map;


/**
 * Created by Administrator(林冠宏) on 2016/2/2.
 *
 */

public class BaseWebview extends WebView {

    private WebSettings setting = null;
    private boolean isInit= false;
    private String htmls = "";
    public static String webHead = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n" +
            "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"zh-CN\" dir=\"ltr\">\n" +
            "<head>\n" +
            "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n" +
            "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=EmulateIE7\" />\n" +
            "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"/>";

    public BaseWebview(Context context) {
        super(context);

    }

    public BaseWebview(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public BaseWebview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

    }

    public void setSetting(WebSettings setting){
        this.setting = setting;
    }

    public interface WebClientListener{
        public WebChromeClient setWebChromeClient();
        public WebViewClient setWebViewClient();
    }

    private WebClientListener webClientListener;
    public void setWebClientListener(WebClientListener webClientListener){
        this.webClientListener = webClientListener;
    }

    @Override
    public void loadUrl(String url) {
        init(); /** 放在这里初始化配置是因为 一开始还没配置就会 执行掉 init */
        super.loadUrl(url);
    }

    private void init(){
        if(!isInit){
            isInit =true;
            if(setting == null){
                setDefaultSetting();
            }
            if(webClientListener==null){
                setWebChromeClient(new WebChromeClient());
                setWebViewClient(new WebViewClient());
            }else{
                setWebChromeClient(webClientListener.setWebChromeClient());
                setWebViewClient  (webClientListener.setWebViewClient());
            }
        }

    }

    private void setDefaultSetting(){
        WebSettings setting = getSettings();
        setting.setDomStorageEnabled(true);
        setting.setCacheMode(WebSettings.LOAD_NO_CACHE);
        setting.setDatabaseEnabled(true);
        setting.setPluginState(WebSettings.PluginState.ON);
        // 全屏显示
        //setting.setUseWideViewPort(true);
        setting.setAllowFileAccess(true);
        setting.setJavaScriptEnabled(true);
        setting.setDefaultTextEncodingName("UTF -8") ;

        // 启用localStorage 和 essionStorage
        //setting.setDomStorageEnabled(true);

        // 开启应用程序缓存
        setting.setAppCacheEnabled(true);
        String appCacheDir = common.context
                .getDir("cache", Context.MODE_PRIVATE).getPath();
        setting.setAppCachePath(appCacheDir);
        setting.setCacheMode(WebSettings.LOAD_DEFAULT);
        setting.setAppCacheMaxSize(1024 * 1024 * 10);// 设置缓冲大小，我设的是10M
        //自适应屏幕
        setting.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        setting.setUseWideViewPort(true);// 这个很关键
        setting.setLoadWithOverviewMode(true);
        setting.setTextSize(WebSettings.TextSize.NORMAL);
    }

    @Override
    public void loadUrl(String url, Map<String, String> additionalHttpHeaders) {
        super.loadUrl(url, additionalHttpHeaders);

    }

    public void defaultLoadDataWithBaseURL(String htmls){ /** 默认 */
        /** utf8 */
        init(); /** 放在这里初始化配置是因为 一开始还没配置就会 执行掉 init */
        loadDataWithBaseURL
                (
                        null,
                        getHtmls(htmls),
                        "text/html",
                        "UTF-8",
                        null
                );
    }

    public void defaultLoadDataWithBaseURL(String htmls,boolean noChange){ /** 默认 */
        /** utf8 */
        init(); /** 放在这里初始化配置是因为 一开始还没配置就会 执行掉 init */
        loadDataWithBaseURL
                (
                        null,
                        htmls,
                        "text/html",
                        "UTF-8",
                        null
                );
    }

    private String getHtmls(String content){

        /** webView 内容头部，已加适配 */



        return webHead +"<style>"+
                "img{" +
                "max-width:100%;" +
                "height:auto}" +
                "table{" +
                "max-width:100%;" +
                "height:auto}"+
                /*"body{" +
                "max-width:100%;" +
                "height:auto}"+
                "div{" +
                "max-width:100%;" +
                "height:auto}"+*/
                "video{"+
                "max-width:100%;"+
                "height:auto}"+
                "</style>" +
                "<link rel=\"stylesheet\" href=\"file:///android_asset/myresorce/style.css\" type=\"text/css\"/>"+
                "<script type=\"text/javascript\" src=\"file:///android_asset/myresorce/jquery.js\"></script>" +
                "<script type=\"text/javascript\" src=\"file:///android_asset/myresorce/function.js\"></script>"+
                "</head>" +
                "<body>" +
                "<div width="+ common.phoneWidth+ "class=\"content_text_board\">"+
                        content+
                "</div>" +
                "<script type=\"text/javascript\">\n"+
                "var tables = document.getElementsByTagName('table');" +
                "for(var i = 0; i<tables.length; i++){" +
                "tables[i].style.width = '100%';" +
                "tables[i].style.height = 'auto';" +
                "}" +
                "</script>\n"+
                "</body>" +
                "</html>";
    }
}
